package priv.pront.code.lanqiao.competition.course.date0403;

public class test {

    static int[] arr = {1, 2, 2, 2, 2, 2, 4, 5, 6, 7, 8, 9, 11, 15, 17, 19};
    static int target = 10, left, right;

//     这两个模板适合于数组里面一定存在多个该数字
    public static void main(String[] args) {
        bSearch_1();
        bSearch_2();
    }

//    目标值的最小位置
    private static void bSearch_1() {
        left = 0;
        right = arr.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] >= target) {
                right = mid;
            }else{
                left = mid + 1;
            }
        }
        System.out.println(left);

    }

//    目标值的最大位置
    private static void bSearch_2(){
        left = 0;
        right = arr.length - 1;
        while (left < right) {
            int mid = (left + right + 1) / 2;
            if(arr[mid] <= target){
                left = mid;
            }else{
                right = mid - 1;
            }
        }
        System.out.println(left);
    }


}
